<template>
  <div class="dashboard-container">
    <div class="container">
      <el-button
        type="primary"
        style="margin-left: 15px"
        @click="addCarousel('add')"
      >
        + 新增
      </el-button>
      <el-table
        v-if="tableData.length"
        :data="tableData"
        stripe
        class="tableBox"
      >
        <el-table-column prop="id" label="id" />
        <el-table-column prop="image" label="图片">
          <template slot-scope="{ row }">
            <el-image
              style="width: 80px; height: 40px; border: none; cursor: pointer"
              :src="row.image"
            >
              <div slot="error" class="image-slot">
                <img
                  src="./../../assets/noImg.png"
                  style="width: auto; height: 40px; border: none"
                />
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="description" label="描述" />
        <el-table-column label="操作" width="250" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              class="blueBug"
              @click="addCarousel(scope.row)"
              >修改</el-button
            >
            <el-button
              type="text"
              size="small"
              class="delBut"
              @click="deleteHandle('单删', scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
  
<script lang="ts">
import { Component, Vue } from 'vue-property-decorator'
import HeadLable from '@/components/HeadLable/index.vue'
import { deleteCarousel, getCarouselPage } from '@/api/carousel.ts'
import InputAutoComplete from '@/components/InputAutoComplete/index.vue'
import Empty from '@/components/Empty/index.vue'

@Component({
  name: 'DishType',
  components: {
    HeadLable,
    InputAutoComplete,
    Empty,
  },
})
export default class extends Vue {
  private input: any = ''
  private counts: number = 0
  private page: number = 1
  private pageSize: number = 10
  private tableData: [] = []
  private isSearch: boolean = false
  private checkList: any[] = []
  created() {
    this.init()
  }

  initProp(val) {
    this.input = val
    this.initFun()
  }

  initFun() {
    this.page = 1
    this.init()
  }
  private addCarousel(st: any) {
    if (st === 'add') {
      this.$router.push({
        path: '/carousel/add',
      })
    } else {
      this.$router.push({
        path: '/carousel/add',
        query: { id: st.id },
      })
    }
  }
  // 删除
  private deleteHandle(type: string, id: any) {
    if (type === '批量' && id === null) {
      if (this.checkList.length === 0) {
        return this.$message.error('请选择删除对象')
      }
    }
    this.$confirm('确定删除该材料?', '确定删除', {
      confirmButtonText: '删除',
      cancelButtonText: '取消',
      type: 'warning',
    }).then(() => {
      deleteCarousel(type === '批量' ? this.checkList.join(',') : id)
        .then((res) => {
          if (res.data.code === 1) {
            this.$message.success('删除成功！')
            this.init()
          } else {
            this.$message.error(res.data.msg)
          }
        })
        .catch((err) => {
          this.$message.error('请求出错了：' + err.message)
        })
    })
  }
  private async init(isSearch?) {
    this.isSearch = isSearch
    await getCarouselPage({
      page: this.page,
      pageSize: this.pageSize,
      name: this.input || undefined,
    })
      .then((res) => {
        if (res.data.code === 1) {
          this.tableData = res.data && res.data.data && res.data.data.records
          this.counts = Number(res.data.data.total)
        }
      })
      .catch((err) => {
        this.$message.error('请求出错了：' + err.message)
      })
  }
}
</script>
<style scoped>
.container {
  border-radius: 30px;
}
</style>